<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
//	function _initialize(){
//		//初始化
//	}
	public function index() {
		$Article = M('Article');
		$ArticleType = M('ArticleType');
		$Banner = M('Banner');
		$ids = $ArticleType -> where('status = 1') -> getField('id', true);
		$map['type'] = array('IN', $ids);
		$map['status'] = 1;
		$count = $Article -> where($map) -> count();
		$Page = new \Think\PageBootstrap($count, 5);
		$Page -> setConfig('prev',"上一页");
		$Page -> setConfig('next',"下一页");
		$show = $Page -> show();
		$list = $Article -> where($map) -> order('id desc') -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
		$hot = $Article -> where($map) -> order(array('read'=>'desc')) -> limit(10) -> select();
		if (!$list) {
			$this -> error($Article -> getError());
		} else {
			$this -> assign("list", $list);
			$this -> assign('page', $show);
			$this -> assign('hot', $hot);
			$this -> assign('bs', $Banner -> where('status = 1') -> order('sort') -> select());
			$this -> display();
		}
	}

	public function detail() {
		$a_id = I("get.a_id");
		$Article = M('Article');
		$data = $Article -> find($a_id);
		$Article -> read += rand(1, 20);
		$Article -> save();
		$hot = $Article -> where('type=%d AND status=%d', array($data['type'],1)) -> order(array('read'=>'desc')) -> limit(10) -> select();
		$channel = M('ArticleType') -> find($data['type']);
		$this -> assign("data", $data);
		$this -> assign("hot", $hot);
		$this -> assign("channel", $channel['name']);
		$this -> display();
	}

	public function channel() {
		$type = I('get.id');
		$channel = M('ArticleType') -> find($type);
		$Article = M('Article');
		$count = $Article -> count();
		$Page = new \Think\PageBootstrap($count, 1);
		$Page -> setConfig('prev',"上一页");
		$Page -> setConfig('next',"下一页");
		$show = $Page -> show();
		$list = $Article -> where('type=%d AND status=%d', array($type,1)) -> order('id desc') -> select();
		$hot = $Article -> where('type=%d AND status=%d', array($type,1)) -> order(array('read'=>'desc')) -> limit(10) -> select();
		if (!$list) {
			$this -> error($Article -> getError());
		} else {
			$this -> assign("list", $list);
			$this -> assign("channel", $channel);
			$this -> assign('page', $show);
			$this -> assign('hot', $hot);
			$this -> display();
		}
	}

}
